package cn.line5.organization;

import cn.line5.login.LoginService;
import cn.line5.seam.annotations.AjaxRemote;
import cn.line5.seam.annotations.In;
import cn.line5.seam.annotations.Name;
import cn.line5.seam.annotations.Out;
import cn.line5.seam.annotations.Parameter;
import cn.line5.seam.contexts.UploadContext;
import cn.line5.seam.db.PageResult;
import cn.line5.seam.webapp.UploadResult;

@Name("organizationRemote")
/**
 * <h1>机构用户管理组件调用类</h1>
 * <br/>
 * 
 * @author Liu,Xiaojun
 */
public class OrganizationRemote {

	@Parameter
	// 当前页数
	private String currentPage;

	@Parameter
	// 权限模块ID
	private String[] permissionModuleIds;

	@Parameter
	// 权限功能ID
	private String[] permissionFunctionIds;

	@Parameter
	@Out
	// 机构ID
	private String organizationId;

	@Parameter
	// 机构ID数组
	private String[] organizationIds;

	@Parameter
	// 用户ID
	private String userId;

	@Parameter
	// 用户ID数组
	private String[] userIds;

	@Parameter
	// 新机构ID
	private String newOrganizationId;

	@Out
	// 分页列表
	private PageResult pageResult;

	@Out
	// 机构详细信息
	private OrganizationVo organization;

	@Out
	@Parameter
	// 用户详细信息
	private UserVo user;

	@Out
	// 机构树的JS代码
	private String treeJs;

	@Out
	// 机构下拉列表的HTML代码
	private String selectHtml;

	@Out
	// 设置权限模块功能的HTML代码
	private String permissionHtml;

	@In("organizationService")
	private OrganizationService organizationService;
	
	@In("loginService")
	private LoginService loginService;

	/**
	 * 获取机构树
	 */
	public void organizationTree() {
		treeJs = organizationService.getOrganizationTreeJs();
	}

	/**
	 * 机构列表
	 */
	public void listOrganization() {
		pageResult = organizationService.getOrganizationPageList(
				organizationId, currentPage);
	}

	/**
	 * 机构主页
	 */
	public void organizationMain() {
		organization = organizationService.getOrganization(organizationId);
	}

	/**
	 * 机构信息
	 */
	public void organizationInfo() {
		organization = organizationService.getOrganization(organizationId);
	}

	/**
	 * 添加机构
	 */
	@AjaxRemote
	public Boolean addOrganization(OrganizationVo vo) {
		return organizationService.addOrganization(vo);
	}

	/**
	 * 编辑机构页面
	 */
	public void organizationEdit() {
		organization = organizationService.getOrganization(organizationId);
	}

	/**
	 * 修改机构
	 */
	@AjaxRemote
	public Boolean modifyOrganization(OrganizationVo vo) {
		return organizationService.modifyOrganization(vo);
	}

	/**
	 * 机构向上排序
	 */
	@AjaxRemote
	public Boolean upOrganization(String organizationId) {
		return organizationService.upOrganization(organizationId);
	}

	/**
	 * 机构向下排序
	 */
	@AjaxRemote
	public Boolean downOrganization(String organizationId) {
		return organizationService.downOrganization(organizationId);
	}

	/**
	 * 删除机构
	 */
	public void deleteOrganization() {
		organizationService.deleteOrganization(organizationIds);
	}

	/**
	 * 用户列表
	 */
	public void listUser() {
		pageResult = organizationService.getUserPageList(organizationId,
				currentPage);
		selectHtml = organizationService.getOrganizationSelectHtml("", "");
	}

	/**
	 * 添加用户
	 */
	public void addUser() {
		organizationId = user.getOrganizationId();
		UploadResult f = UploadContext.getUploadResult();
		if (f != null && f.isSuccess())
			if (f.getFileNum() > 0)
				user.setPhotoUrl(f.getFileList().get(0).getUploadFileName());
		organizationService.addUser(user);
	}

	/**
	 * 编辑用户页面
	 */
	public void userEdit() {
		user = organizationService.getUser(userId);
	}

	/**
	 * 修改用户
	 */
	public void modifyUser() {
		organizationId = user.getOrganizationId();
		UploadResult f = UploadContext.getUploadResult();
		if (f != null && f.isSuccess())
			if (f.getFileNum() > 0)
				user.setPhotoUrl(f.getFileList().get(0).getUploadFileName());
		organizationService.modifyUser(user);
	}

	/**
	 * 删除用户
	 */
	public void deleteUser() {
		organizationService.deleteUser(userIds);
	}

	/**
	 * 调整用户
	 */
	public void changeOrganization() {
		organizationService.changeOrganization(userIds, newOrganizationId);
	}

	/**
	 * 锁定用户
	 */
	@AjaxRemote
	public Boolean lockUser(String userId) {
		return organizationService.lockUser(new String[] { userId });
	}

	/**
	 * 解除锁定用户
	 */
	@AjaxRemote
	public Boolean unlockUser(String userId) {
		return organizationService.unlockUser(new String[] { userId });
	}

	/**
	 * 初始化用户密码
	 */
	@AjaxRemote
	public Boolean resetUserPassword(String userId) {
		return organizationService.resetUserPassword(new String[] { userId });
	}

	/**
	 * 检测用户名是否被占用
	 */
	@AjaxRemote
	public Boolean checkLoginName(String loginName) {
		return organizationService.checkLoginName(loginName);
	}

	/**
	 * 检测机构下是否有用户
	 */
	@AjaxRemote
	public Boolean checkHaveUser(String[] organizationIds) {
		return organizationService.checkHaveUser(organizationIds);
	}

	/**
	 * 设置用户权限页面
	 */
	public void permissionSet() {
		permissionHtml = organizationService.getModuleHtml("", loginService.getModuleAndFunctionPermission(userId));
	}

	/**
	 * 设置用户权限
	 */
	public void setPermission() {
		organizationService.setPermission(userId, permissionModuleIds,
				permissionFunctionIds);
	}
}
